package com.adxon.data2;

import com.adxon.MsgException;
import com.adxon.data2.Base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

public class DataSet<T> implements Iterable<T>,Iterator<T>{
	protected String			bsk;
	protected Class<?>			cls;
	private Connection			con;
	protected T					nxt;
	private ResultSet			res;
	protected T					tmp;

	public DataSet(T template){
		try{
			bsk=((Base)template).bsk;
			cls=template.getClass();
			tmp=template;
			String q="SELECT * FROM ?";
			PreparedStatement s=con.prepareStatement(q);
			s.setString(1,bsk);
			res=s.executeQuery();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	protected void addFilter(String name,int value){
		if(value!=0)
			;
	}
	protected void addFilter(String name,long value){
		if(value!=0)
			;
	}
	protected void addFilter(String name, String value) {
		if(value!=null)
			;
	}
	public T getTemplate(){
		return tmp;
	}
	@SuppressWarnings("unchecked")
	@Override
	public boolean hasNext(){
		//System.out.println("hasNext: "+nxt);
		if(nxt==null){
			try{
				while(res.next()){
					nxt=(T)((Data)cls.newInstance()).update(res);
					return true;
				}
				nxt=null;
			}catch(MsgException e){
				e.printStackTrace();
			}catch(SQLException e){
				e.printStackTrace();
			}catch(IllegalAccessException e){
				e.printStackTrace();
			}catch(InstantiationException e){
				e.printStackTrace();
			}
		}else return true;
		return false;
	}
	@Override
	public Iterator<T> iterator(){
		return this;
	}
	@Override
	public T next(){
		T r=nxt;
		nxt=null;
		return r;
	}
	@Override
	public void remove(){
	}
}